//
//  VistorView.swift
//  WeiboSwift
//
//  Created by Apple13 on 16/7/9.
//  Copyright © 2016年 yupeng. All rights reserved.
//

import UIKit
import SnapKit


class VistorView: UIView {

    //MARK: - 设置闭包
    var loginClosure: (()->())?
    
    //MARK: - 加载控件
    
    
    /// 📌旋视图
    private lazy var cycleImage: UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_image_smallicon"))
    
    /// 📌罩层
    private lazy var maskIconImageView:UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_mask_smallicon"))
    
    
    private lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))

    
    ///📌注册按钮
    private lazy var registerButton:UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "registerAction", forControlEvents: .TouchUpInside)
//‼️还未添加事件
        //设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        //设置文本及文本颜色
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        
        return button
    }()
    
    
    /// 📌登录按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
//‼️还未添加事件
        //设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        //设置文本及文本颜色
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)

        return button
    }()
    
    ///  📌消息Label
    private lazy var messageLabel:UILabel = {
       let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(15)
        label.text = "你认识的人,你读过的书,你经历过的事情,决定你的一生"
        label.textColor = UIColor.redColor()
        //多行显示
        label.numberOfLines = 0
        //居中显示
        label.textAlignment = .Center
        return label
        
    }()
    
//MARK: - 构造函数
    // 使用手写代码的方式去调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
//        startAnimation()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
//        fatalError("init(coder:) has not been implemented")
        backgroundColor = UIColor.redColor()
        //‼️设置ui ------此处设置不生效
        setupUI()
    }
    
    
    private func setupUI() {
        
        backgroundColor = UIColor(white: 237/255,alpha:1)
        
        //添加控件
        addSubview(cycleImage)
        addSubview(maskIconImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //用第三方控件设置frame
        cycleImage.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        maskIconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImage)
        }
        
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImage)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.top.equalTo(cycleImage.snp_bottom)
            make.centerX.equalTo(cycleImage)
            make.width.equalTo(225)
        }
        
        
        registerButton.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }
    }
    
    
    
    func updateVistorInfo(message:String?, imageName:String?) -> () {
        if let msg = message,imgName = imageName{//非首页
            messageLabel.text = msg
            iconImageView.image = UIImage(named: imgName)
            //隐藏旋转视图
            cycleImage.hidden = true
        }else{//是首页
            
            cycleImage.hidden = false
            startAnimation()
        }
    }
    
    
    
    
    //MARK: - 添加点击事件
    
    @objc private func registerAction(){
        print("--------点击中间的注册")
        loginClosure?()
    }
    
    @objc private func loginAction(){
        print("---------点击了中间的登录")
        loginClosure?()
    }
    
    //MARK: - 开启旋转动画
    private func startAnimation(){
        let animation = CABasicAnimation(keyPath:"transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        animation.duration = 20
        
        animation.repeatCount = MAXFLOAT
        
        //‼️不释放资源
        animation.removedOnCompletion = false
        
        //执行动画
        cycleImage.layer.addAnimation(animation, forKey: nil)
        
    }
    
    
    
}
